
var keystone = require('keystone');
var Promise = require("bluebird");
var ObjectId = require('mongoose').Types.ObjectId;


const isNumber = (o) => { //是否数字  
    return Object.prototype.toString.call(o).slice(8, -1) === 'Number'
}
exports.submit_user_message = function (req, res) {

    let new_data = {};

    if (req.body.permission !== 'null' &&
        req.body.permission !== null &&
        req.body.permission !== "undefined" &&
        req.body.permission !== undefined &&
        isNumber(parseInt(req.body.permission))
    ) {
        new_data.permission = parseInt(req.body.permission);
    }

    keystone.list('UserProfile').model
        .findByIdAndUpdate(new ObjectId(req.body.user_profile_id), new_data)
        .exec((err, ) => {
            if (err) {
                console.log(err);
                return res.json({ err: 1 });
            } else {
                res.json({ err: 0 });
            }
        });
}

exports.user_remove = function (req, res) {

    Promise.all([
        keystone.list('User').model
            .findByIdAndRemove(new ObjectId(req.body.user_id))
            .exec(),
        keystone.list('UserProfile').model
            .findOneAndRemove({ user: new ObjectId(req.body.user_id) })
            .exec(),

    ])
        .then(() => {
            return res.json({ err: 0 })

        })
        .catch((err) => {
            throw err;
        });


}

